<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      姓：<input v-model="firstName" type="text" /><br />
      名：<input v-model="lastName" type="text" /><br />
      <p>姓名：{{ fullName }}</p>
      <button @click="changeFullName">修改姓名</button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          firstName: "",
          lastName: "",
        },
        computed: {
          // 简写的形式只能被访问, 不能被修改
          // fullName() {
          //   return this.firstName + this.lastName;
          // },
          // 如果想要既能访问也能修改, 请用完整写法
          fullName: {
            get() {
              return this.firstName + this.lastName;
            },
            set(value) {
              console.log("修改后的结果", value);
              this.firstName = value.slice(0, 1);
              this.lastName = value.slice(1);
            },
          },
        },
        methods: {
          changeFullName() {
            console.log(this.fullName);
            // 修改计算属性的值
            this.fullName = "张三";
          },
        },
      });
    </script>
  </body>
</html>
